package pack1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swim.entity.RegisteredUser;
import com.swim.session.RegisteredUserRemote;

public class ResultsServlet extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 3369584356067204317L;

	public ResultsServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		if(!"skill".equals(type)){
			response.sendRedirect("index.jsp");
		} else {
			search(request,response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		search(request, response);
	}
	
	private void search(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");

		String searchType = request.getParameter("type");
		String query = request.getParameter("query");
		String among = request.getParameter("among");
		try {
				InitialContext jndiContext = (InitialContext) Util.getInitialContext();
				Object ref = jndiContext.lookup("RegisteredUserBeanJNDI");
				RegisteredUserRemote r = (RegisteredUserRemote) ref;
				
				List<RegisteredUser> results = new ArrayList<RegisteredUser>();
				if("friends".equals(among)) {
					if("name".equals(searchType)) {
						String names[] = query.split(" ");
						String surname = "";
						if(names.length>1)
							surname = names[1];
						results = r.getFriendsByName((String) request.getSession().getAttribute("authenticated-email"),names[0], surname);
					} else if("city".equals(searchType)) {
						results = r.getFriendsByCity((String) request.getSession().getAttribute("authenticated-email"), query);
					} else if("skill".equals(searchType)) {
						results = r.getFriendsBySkill((String) request.getSession().getAttribute("authenticated-email"), query);
					}
				} else {
					if("name".equals(searchType)) {
						String names[] = query.split(" ");
						String surname = "";
						if(names.length>1)
							surname = names[1];
						results = r.getUsersByName(names[0], surname);
					} else if("city".equals(searchType)) {
						results = r.getUsersByCity(query);
					} else if("skill".equals(searchType)) {
						results = r.getUsersBySkill(query);
					}
				}
				request.setAttribute("results", results);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/results.jsp");
				dispatcher.forward(request, response); 
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}